package cn.waka.sys.admin.service;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.waka.sys.admin.dao.DeviceDao;
import cn.waka.sys.admin.object.Box;
import cn.waka.sys.admin.object.Page;

@Service
public class DeviceService {

    @Autowired
    private DeviceDao deviceDao;
    @Autowired
    private RedisService redisService;

    public Page findBoxPage(Page p, Map<String, String> param) {
        List<Box> list = deviceDao.findBoxList(p, param);
        for (int i = 0; i < list.size(); i++) {
            String rs = redisService.get("_box:" + list.get(i).getId());
            if (rs == null) {
                list.get(i).setIsonline(false);
            } else {
                list.get(i).setIsonline(true);
            }
        }
        int count = deviceDao.findBoxCount(p, param);
        p.setData(list);
        p.setCount(count);
        int totalCount = 1;
        if (count % p.getSize() > 0) {
            totalCount = (int) Math.floor(count / p.getSize());
            totalCount++;
        } else {
            totalCount = count / p.getSize();
        }
        p.setTotalPage(totalCount);
        return p;
    }


}
